package com.xs.ahoi.hcxs.ui.dialog;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.BookDao;
import com.xs.ahoi.hcxs.model.Book;
import com.xs.ahoi.hcxs.utils.Constants;
import com.xs.ahoi.hcxs.utils.DateUtil;
import com.xs.ahoi.hcxs.utils.FileUtil;
import com.xs.ahoi.hcxs.utils.ToastUtil;

/**
 * 书籍列表长按信息弹窗页面
 */
public class DialogBookInfo extends BaseDialog {

    @BindView(R.id.tvBookName)
    public TextView tvBookName;
    @BindView(R.id.tvBookPath)
    public TextView tvBookPath;
    @BindView(R.id.ivSwitch)
    public ImageView ivSwitch;

    // 书籍查询工具类
    private BookDao bookDao;
    // 书籍ID
    private int bookId;
    private Book book;
    private BookInfoListener bookInfoListener;
    // 置顶图标开启或关闭
    private Drawable switchOn, switchOff;

    // 是否删除弹框
    private DialogCommonDelete dialogCommonDelete;

    // 重命名弹框
    private DialogChangeFileName dialogChangeFileName;

    // 分享弹框
    private DialogShare dialogShare;


    public DialogBookInfo(@NonNull Context context) {
        super(context, R.style.common_dialog);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_book_info;
    }

    @Override
    protected int showPosition() {
        return Gravity.BOTTOM;
    }

    @Override
    protected void initData() {
        bookDao = BookDao.getInstance();

        // 初始化置顶图标
        switchOn = ContextCompat.getDrawable(getContext(), R.drawable.icon_switch_on);
        switchOff = ContextCompat.getDrawable(getContext(), R.drawable.icon_switch_off);

        // 更新视图
//        updateView();

        // 初始化删除提示框
        dialogCommonDelete = new DialogCommonDelete(getContext());
        // 重命名弹框
        dialogChangeFileName = new DialogChangeFileName(getContext());
        // 分享弹框
        dialogShare = new DialogShare(getContext());
    }

    @Override
    protected void initListener() {
        dialogCommonDelete.setDeleteListener(isDeleteLocal -> {
            // 删除本地
            if (isDeleteLocal) {
                FileUtil.delete(book.getPath());
            }

            bookDao.delete(book);
            ToastUtil.show("已删除");
            bookInfoListener.refresh();
        });
        dialogChangeFileName.setListener((newPath, newFilename) -> {
            // 更新文件名和路径
            book.setPath(newPath);
            book.setName(newFilename);
            bookDao.updatePath(bookId, book);
            ToastUtil.show("修改成功");
            bookInfoListener.refresh();
        });
        dialogShare.setShareListener(intent -> {
            bookInfoListener.startActivity(intent);
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        updateView();
    }

    // 更新视图
    private void updateView() {
        book = bookDao.getById(bookId);
        tvBookName.setText(book.getName());
        tvBookPath.setText(book.getPercent() + "  " + book.getPath());

        // 设置置顶图标
        if (book.getSetTopTime() == 0) {
            ivSwitch.setImageDrawable(switchOff);
        } else {
            ivSwitch.setImageDrawable(switchOn);
        }
    }

    @OnClick({R.id.btnRename, R.id.btnSetTop, R.id.btnDelete, R.id.btnShare})
    public void onClick(View v) {
        switch (v.getId()) {
            // 重命名
            case R.id.btnRename:
                dialogChangeFileName.show(book.getName(), book.getPath());
                dismiss();
                break;
            // 置顶
            case R.id.btnSetTop:
                long setTopTime = 0;
                if (book.getSetTopTime() == 0) {
                    setTopTime = DateUtil.getTime();
                }
                bookDao.updateSetTop(bookId, setTopTime);
                updateView();

                // 刷新列表
                bookInfoListener.refresh();
                break;
            // 删除
            case R.id.btnDelete:
                dialogCommonDelete.show();
                dialogCommonDelete.showCheckbox();
                dismiss();
                break;
            // 分享
            case R.id.btnShare:
                dialogShare.show();
                dialogShare.setShareType(Constants.ShareType.file);
                dialogShare.setFilePath(book.getPath());
                dismiss();
                break;
        }
    }


    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public void setBookInfoListener(BookInfoListener bookInfoListener) {
        this.bookInfoListener = bookInfoListener;
    }

    public interface BookInfoListener {
        void refresh();

        void startActivity(Intent intent);
    }
}
